#include "NightLightController.h"

#include <QProcess>
#include <QThread>
#include <QtDBus>
#include <QTimer>

NightLightController::NightLightController(QObject *parent) : QObject(parent)
{
}

NightLightController::~NightLightController()
{
}

void NightLightController::toDarkMode()
{
    // // 等待 gsd-color 服务响应，这是解决问题的关键
    // QThread::msleep(1000);
    turnOffScreen();
    setGSettingsValue("org.gnome.settings-daemon.plugins.color", "night-light-enabled", true);
    setGSettingsValue("org.gnome.settings-daemon.plugins.color", "night-light-temperature", DARK_MODE_KELVIN);

    QThread::msleep(1000);
    turnOnScreen();
}

void NightLightController::toLightMode()
{
    turnOffScreen();
    setGSettingsValue("org.gnome.settings-daemon.plugins.color", "night-light-enabled", false);
    QThread::msleep(2000);
    turnOnScreen();
}

void NightLightController::turnOffScreen()
{
    const QString service = "org.gnome.Mutter.DisplayConfig";
    const QString path = "/org/gnome/Mutter/DisplayConfig";
    const QString interface = "org.freedesktop.DBus.Properties";
    const QString propertyName = "PowerSaveMode";

    QDBusInterface dbusInterface(service, path, interface, QDBusConnection::sessionBus());
    if (!dbusInterface.isValid()) {
        qWarning() << "D-Bus interface is not valid:" << service;
        return;
    }

    // PowerSaveMode: 3 = Off, 0 = On
    const qint32 modeOff = 3;
    const qint32 modeOn = 0;

    auto setMode = [&](qint32 mode) {
        QVariant propertyValue = QVariant::fromValue(QDBusVariant(QVariant::fromValue(mode)));
        dbusInterface.call("Set", service, propertyName, propertyValue);
    };

    // 关屏
    setMode(modeOff);
}

void NightLightController::turnOnScreen()
{
    const QString service = "org.gnome.Mutter.DisplayConfig";
    const QString path = "/org/gnome/Mutter/DisplayConfig";
    const QString interface = "org.freedesktop.DBus.Properties";
    const QString propertyName = "PowerSaveMode";

    QDBusInterface dbusInterface(service, path, interface, QDBusConnection::sessionBus());
    if (!dbusInterface.isValid()) {
        qWarning() << "D-Bus interface is not valid:" << service;
        return;
    }

    // PowerSaveMode: 3 = Off, 0 = On
    const qint32 modeOff = 3;
    const qint32 modeOn = 0;

    auto setMode = [&](qint32 mode) {
        QVariant propertyValue = QVariant::fromValue(QDBusVariant(QVariant::fromValue(mode)));
        dbusInterface.call("Set", service, propertyName, propertyValue);
    };

    // 开屏 (按你的要求，中间不延迟)
    setMode(modeOn);
}

bool NightLightController::setGSettingsValue(const QString &schema, const QString &key, const QVariant &value)
{
    QProcess process;
    QStringList args{"set", schema, key};

    // QVariant::toString() 对 bool 会转成 "true"/"false"，对 int 转成数字字符串，正好适用
    args << value.toString();

    process.start("gsettings", args);
    if (!process.waitForFinished(1000)) { // 2秒超时
        qWarning() << "gsettings command timed out.";
        process.kill();
        return false;
    }

    if (process.exitCode() != 0) {
        qWarning() << "gsettings command failed:" << process.readAllStandardError();
        return false;
    }
    return true;
}
